package com.inspur.edp.bff.core.manager;

import com.inspur.edp.bef.core.session.FuncSession;
import com.inspur.edp.bff.core.buffer.BffBufferManager;
import com.inspur.edp.bff.core.changeset.BffChangeManager;
import com.inspur.edp.cef.api.dataType.base.IAccessorCreator;
import com.inspur.edp.cef.entity.accessor.base.IAccessor;
import com.inspur.edp.cef.entity.changeset.ChangeType;
import com.inspur.edp.cef.entity.changeset.IChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;

import java.util.Map;

public class BffBufferChangeManager implements IBffBufferChangeManager {
    private FuncSession session;
    private String entityType;
    private BffBufferManager bufferManager;
    private BffChangeManager changeManager;

    public BffBufferChangeManager(FuncSession session, String enyityType, IAccessorCreator accessorCreator) {

        this.session = session;
        this.entityType = enyityType;
        this.bufferManager = new BffBufferManager(session, entityType, accessorCreator);
        this.changeManager = new BffChangeManager(session, entityType);
    }

    @Override
    public IEntityData getCurrentData(String dataId) {
        return bufferManager.getCurrentData(dataId);
    }

    @Override
    public IEntityData getOriginalData(String dataId) {
        return bufferManager.getOriginalData(dataId);
    }

    @Override
    public IAccessor initCurrentBuffer(IEntityData data) {
        return bufferManager.initCurrentBufferData(data, false);
    }

    @Override
    public IAccessor initOriginalBuffer(IEntityData data) {
        return bufferManager.initOriginalBufferData(data, true);
    }

    @Override
    public IChangeDetail getCurrentChange(String dataId) {
        return changeManager.getCurrentChange(dataId);
    }

    @Override
    public Map<String, IChangeDetail> getCurrentChange() {
        return changeManager.getCurrentChange();
    }


    @Override
    public void appendCurrentTemplateChange(IChangeDetail change) {
        changeManager.appendCurrentTemplateChange(change);
    }

    @Override
    public void acceptCurrentChange(String dataId, IChangeDetail change) {
        if (change != null && change.getChangeType() != ChangeType.Added) {
            bufferManager.acceptCurrentChange(dataId, change);
        }
    }

    @Override
    public void clear() {
        bufferManager.clear();
        changeManager.clear();
    }

}
